package com.baidu.thirdlib.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.baidu.thirdlib.BaseActivity;
import com.baidu.thirdlib.R;
import com.kvin.toolkit.utils.DialogUtils;
import com.kvin.toolkit.widget.LoadingDialog;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ToolKitActivity extends BaseActivity {

    @BindView(R.id.start_btn)
    Button startBtn;
    @BindView(R.id.stop_btn)
    Button stopBtn;


    private LoadingDialog mDialog;
    private Timer mTimer;
    private int limit = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_kit);
        ButterKnife.bind(this);
    }


    @Override
    public void init() {
        startBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);

        mDialog = LoadingDialog.getLoadingDialog(this);
//        mDialog.setLoadingImage(R.mipmap.loading_ios);


        mTimer = new Timer();

//        mDialog.setLayoutParams(200,200);

    }


    @Override
    public void initNav() {
        setTitle("ToolKit");
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_btn:
                limit=10;
                mDialog.show();
                mTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        mHandler.sendEmptyMessage(limit--);
                    }
                }, 0, 1000);
                break;
            case R.id.stop_btn:
                mDialog.dismiss();
                break;
        }
    }

    @Override
    public void onHandleMessage(Message msg) {
        if (msg.what == -1) {
            mDialog.dismiss();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }

    }
}
